#include<regx52.h>
sbit   set=P3^3;
sbit   up=P3^2;
sbit   down=P3^1;

sbit s1=P1^5;     // s1  muc 0 la muc sang
sbit s2=P1^4;     //s2
sbit m1=P1^3;     //m1
sbit m2=P1^2;     //m2
sbit h1=P1^1;     //h1
sbit h2=P1^0;     //h2
unsigned char hour, minute, second;
int mode;
unsigned char ma[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char tp; // phuc vu cho nhay 
bit nhay;
sbit SCL = P3^5;
sbit SDA = P3^4;   

void nop()
{
	unsigned char i;
	i=6;
	while(i)
	{
		i--;
	}
}

void delay(int t)
{
	int i;
	for(i=0;i<t;i++);
}

void quetled()
{ 
	P0=ma[second&0x0f];
	if(mode==3)
		s1=nhay;
	else
		s1=1;
	delay(100);
	s1=0;
	
	P0=(ma[(second>>4)&0x0f]);
	if(mode==3)
		s2=nhay;	
	else
		s2=1;
	delay(100);
	s2=0;
	
	P0=ma[minute&0x0f];
	if(mode==2)
		m1=nhay;	
	else
		m1=1;
	delay(100);
	m1=0;
	
	P0=(ma[(minute>>4)&0x0f]);
	if(mode==2)
		m2=nhay;	
	else
		m2=1;
	delay(100);
	m2=0;
	

	P0=(ma[hour&0x0f]);
	if(mode==1)
		h1=nhay;	
	else
		h1=1;
	delay(100);
	h1=0;
	
	P0=(ma[(hour>>4)&0x0f]);
	if(mode==1)
		h2=nhay;	
	else
		h2=1;
	delay(100);
	h2=0; 	
}

void delay_ms(int t)
{
	int i;
	for(i=0;i<t;i++)
		quetled();
}

void start_I2C()
{  
	SCL = 1; 
	SDA = 1;
	nop();
	nop(); 
	SDA = 0; 
	SCL = 0;
	nop();
	nop(); 
}

void stop_I2C()
{  
	SCL = 1;
	SDA = 0;
	nop();
	nop(); 
	SDA = 1; 
}

//write I2C 
void write_I2C(unsigned char data2send)
{ 
	int i; 
	for (i=0;i<8;i++)
	{  
		SDA = (data2send & 0x80) ? 1:0; SCL=1;
		nop();
		SCL=0;
		data2send<<=1;
		nop(); 
	} 
	//clock to receive ACK from slave 
	SCL = 1;
	nop();
	nop(); 
	SCL = 0; 
}

unsigned char read_I2C(bit ACK_Bit)
{ 
	unsigned char Data=0;
	int i; 
	SDA = 1; 
	for (i=0;i<8;i++)
	{  
		SCL = 1; 
		Data<<= 1; 
		Data = (Data | SDA); 
		SCL = 0;
		nop(); 
	}
	if (ACK_Bit == 1) 
	SDA = 0; // Send ACK 
	else 
	SDA = 1; // Send NO ACK 
	nop();
	nop(); 
	//clock to send (N)ACK  
	SCL = 1;
	nop();
	nop(); 
	SCL = 0;
	return Data; 
}
 
//-------------------------------------------------------------
void rtc_write(unsigned char add, unsigned char dat){
start_I2C();
write_I2C(0xd0);
write_I2C(add); 
write_I2C(dat); 
stop_I2C();
}

unsigned char rtc_read(unsigned char add)
{
	unsigned char dat;
	start_I2C();
	write_I2C(0xd0); 
	write_I2C(add);
	start_I2C(); 
	write_I2C(0xd1); 
	dat = read_I2C(0);
	stop_I2C();	
	//dat = dat&0x0f+(dat>>4)*10;
	return (dat);
}
void writeds()
{
	rtc_write(0x00, second);
	rtc_write(0x01, minute);
	rtc_write(0x02, hour);
}

void readds()
{  
	
	second = rtc_read(0x00);
	minute = rtc_read(0x01);
	hour = rtc_read(0x02); 	

}
//////////////////////////// 

unsigned char tang(unsigned char cur,unsigned char max)
{	    	
	if(cur>=max)
		return 0x00;
	if((cur&0x0f) == 0x09)
		return cur+0x07;
	return cur+1;
}

unsigned char giam(unsigned char cur,unsigned char max)
{
    if(cur==0x00)
		return max;
	if((cur&0x0f) == 0x00)
		return cur-0x07;	
	return cur-1;
}

void ktphim()
{
	if(set==0)
	{
		mode+=1;
		if(mode>4) mode =0;
		if(mode==4) 
		{
			writeds();
			mode=0;
		}
		delay_ms(50);
	}	
	switch(mode)
	{
		case 1: // chinh gio
			if(up==0)			
				hour = tang(hour,0x23);				
			if(down ==0)
				hour = giam(hour,0x23);
			break;
		case 2: // chinh phut		
			if(up==0)
				minute = tang(minute,0x59);
			if(down ==0)
				minute = giam(minute,0x59);
			break;
		case 3: // chinh phut		
			if(up==0)
				second = tang(second,0x59);
			if(down ==0)
				second = giam(second,0x59);
			break;
	}	
	if(up==0||down==0) delay_ms(50);
}
void main()
{
    start_I2C();
    write_I2C(0xD0);	
    write_I2C(0x07); 
    write_I2C(0x10); 
    stop_I2C(); 	   
	mode = 0;
	tp = 0;
	nhay=0;

	TMOD=0x10;     //timer 0 che do 2
    TL1=0x00;               
    TH1=0x00;              
    TR1=1;        //khoi dong timer0
    IE=0x88;  	 // cho phep ngat
	while(1)
	{ 	
		ktphim();		
		if(mode ==0)
		 	readds();	
		quetled();	
	}
}

void songvuong(void) interrupt 3
{

	TR1=0;
	TH1=0x00;
	TL1=0x00;
	tp++;
	if(tp>3)
	{
		nhay=~nhay;
		tp=0;
	}
	
	TR1=1;	
}